﻿@namespace AntDesign
@using Microsoft.AspNetCore.Components.Rendering
@inherits AntDomComponentBase

@code{
    internal void RenderTab(RenderTreeBuilder __builder)
    {
        if (_hasClosed)
            return;

        @if (Parent.Draggable)
        {
            var ondragoverPreventDefault = _isActive;

            <div style=" @TabStyles" 
                @ondragover:preventDefault="@ondragoverPreventDefault" draggable="@Parent.Draggable.ToString()"
                @ondragstart="@(EventUtil.AsNonRenderingEventHandler<DragEventArgs>(e => Parent.HandleDragStart(e, this)))"
                 @ondrop="@(()=> Parent.HandleDrop(this))">
                 @RenderTabInner
            </div>
        }
        else
        {
            @RenderTabInner
        }
    }

    internal void RenderTabInner(RenderTreeBuilder __builder)
    {
        var onclickStopPropagation = !_isActive;

        <div @key="Key"
             @ref="_tabRef"
             @onclick:stopPropagation="@onclickStopPropagation"
             @onclick="@(() => Parent.HandleTabClick(this))"
             class="@_tabClassMapper.Class"
             id="@TabId"
             ondragover="event.preventDefault();">
            <div @onkeydown="@HandleKeydown"
                 @ref="_tabBtnRef"
                 role="tab"
                 aria-selected="@(_isActive ? "true" : "false")"
                 class="ant-tabs-tab-btn"
                 aria-controls="@($"rc-tabs-{Id}-panel-{Key}")"
                 tabindex="0">
                @if (TabTemplate != null)
                {
                    @TabTemplate
                }
                else
                {
                    @Tab
                }
            </div>
            @if (Parent.Type == TabType.EditableCard && Closable)
            {
                <button type="button" aria-label="remove" tabindex="0" class="ant-tabs-tab-remove" @onclick="(e) => Parent.RemoveTab(this)" @onclick:stopPropagation>
                    <Icon Type="close" />
                </button>
            }
        </div>

        @if (TabContextMenu != null)
        {
            <Dropdown @key="@($"tab-menu-{Key}")" Trigger="new[]{ Trigger.ContextMenu }" TriggerReference="_tabRef">
                <Overlay>
                    @TabContextMenu
                </Overlay>
            </Dropdown>
        }
    }

    internal void RenderPane(RenderTreeBuilder __builder)
    {
        if (_hasClosed)
            return;

        <div @key="Key"
             tabindex="@(_isActive ? "0" : "-1")"
             class="@ClassMapper.Class"
             style="@Style"
             id="@($"rc-tabs-{Id}-panel-{Key}")"
             role="tabpanel"
             aria-hidden="@(_isActive ? "false" : "true")"
             aria-labelledby="@($"rc-tabs-{Id}-tab-{Key}")">
            @if (ChildContent != null && (_isActive || _hasRendered || ForceRender))
            {
                _hasRendered = true;
                <AntDesign.Core.Internal.KeepAlive ChildContent="ChildContent" IsActive="@_isActive"></AntDesign.Core.Internal.KeepAlive>
                
            }
        </div>
    }
}